package io.sqrtqiezi.spark.streaming

import org.apache.spark.SparkConf
import org.apache.spark.streaming.{Seconds, StreamingContext}

object SocketDStream {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf()
      .setAppName("spark socket streaming")
      .setMaster("local[*]")

    val ssc = new StreamingContext(conf, Seconds(1))

    val result = ssc.socketTextStream("localhost", 9999)
      .flatMap(_.split("\\s+"))
      .map((_, 1)).reduceByKey(_ + _)

    result.print()

    ssc.start()
    ssc.awaitTermination()
  }
}
